<?php

/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

namespace support;

use voku\helper\AntiXSS;

/**
 * Class Request
 * @package support
 */
class Request extends \Webman\Http\Request
{


    /**
     * $_GET.
     *
     * @param string|null $name
     * @param mixed|null $default
     * @return mixed|null
     */
    public function get(?string $name = null, mixed $default = null): mixed
    {
        if (!isset($this->data['get'])) {
            $this->parseGet();
        }

        $getdata = $this->filter_xss($this->data['get']);
        if (null === $name) {
            return $getdata;
        }
        return isset($getdata[$name]) ? $getdata[$name] : $default;
    }


    /**
     * $_POST.
     *
     * @param string|null $name
     * @param mixed|null $default
     * @return mixed|null
     */
    public function post(?string $name = null, mixed $default = null): mixed
    {

        if (!isset($this->data['post'])) {
            $this->parsePost();
        }
        $postdata = $this->filter_xss($this->data['post']);

        if (null === $name) {
            return $postdata;
        }
        return isset($postdata[$name]) ? $postdata[$name] : $default;
    }

    private function filter_xss($array)
    {
        $result = [];
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                // 如果当前元素是数组，则递归调用
                $result[$key] = $this->filter_xss($value);
            } else {
                // 如果当前元素不是数组，则应用回调函数
                $result[$key] =    htmlspecialchars($value);
            }
        }
        return $result;
    }
}
